//
//  BCServiceTimestampManager.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/15.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCServiceTimestampManager.h"

//
#import "BCTimerHandler.h"


@interface BCServiceTimestampManager () <BCTimerHandlerDelegate>

@property (nonatomic, strong) BCTimerHandler *timerHandler;

@end

@implementation BCServiceTimestampManager

static BCServiceTimestampManager *_serviceTimestampManager = nil;
+ (instancetype)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_serviceTimestampManager) {
            _serviceTimestampManager = [BCServiceTimestampManager new];
        }
    });
    return _serviceTimestampManager;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self.timerHandler start];
    }
    return self;
}
- (BCTimerHandler *)timerHandler {
    if (!_timerHandler) {
        _timerHandler = [[BCTimerHandler alloc] init];
        _timerHandler.timeInterval = 1.0;
        _timerHandler.delegate = self;
    }
    return _timerHandler;
}


- (NSDate *)date {
    if (self.timestamp>0) {
        return [NSDate dateWithTimeIntervalSince1970:self.timestamp];
    }else {
        return [NSDate date];
    }
}


#pragma mark - BCTimerHandlerDelegate
- (void)timerHandlerRepeatsAction {
    if (self.timestamp>0) {
        self.timestamp += _timerHandler.timeInterval;
    }
}







- (void)dealloc {
    if (_timerHandler) {
        [_timerHandler stop];
        _timerHandler = nil;
    }
}

@end
